<?php


namespace app\admin\controller;


use app\admin\model\CategoryModel;
use app\admin\model\GoodsModel;
use app\admin\model\GoodsSpecModel;
use app\admin\model\RoleUserModel;
use app\admin\model\UserModel;
use app\admin\model\WechatUserModel;
use cmf\controller\AdminBaseController;

class GoodsController extends AdminBaseController
{
    public function goods_list()
    {
        $admin_id = cmf_get_current_admin_id();
        $role_id = RoleUserModel::where('user_id', $admin_id)->value('role_id');
        $role_id = $role_id ? $role_id : 1;
        $this->assign('role_id', $role_id);
        if ($this->request->isAjax()) {
            $data = $this->request->param();
            $where = [];
            $num = 10;
            $where[] = ['is_delete', '=', 0];
            if (isset($data['is_on_sale']) && $data['is_on_sale']) {
                $where[] = ['is_on_sale', '=', $data['is_on_sale']];
            }
            if (isset($data['keyword']) && $data['keyword']) {
                $where[] = ['goods_name', 'like', '%' . $data['keyword'] . '%'];
            }
            $admin_id = cmf_get_current_admin_id();
            $admin_info = UserModel::get($admin_id);
            if ($admin_info['company_id']) {
                $where[] = ['company_id', '=', $admin_info['company_id']];
            } else {
                $company_id = isset($data['company_id']) && $data['company_id'] ? $data['company_id'] : 0;
                if ($company_id) {
                    $where[] = ['company_id', '=', $company_id];
                }
            }
            if (isset($data['duration']) && $data['duration']) {
                $where[] = ['create_time', 'between time', [$data['duration'][0] . ' 00:00:00', $data['duration'][1] . ' 23:59:59']];
            }
            $page = isset($data['page']) && $data['page'] ? $data['page'] : 1;
            $list = GoodsModel::with('goods_specs,company_info,category_info')
                ->where($where)
                ->order('is_on_sale desc,sort desc,create_time desc')
                ->paginate($num, false, ['page' => $page])->each(function ($item) {
                    if ($item['goods_img']) {
                        $item['goods_img'] = explode(',', $item['goods_img']);
                    }
                    return $item;
                });
            $this->success('', '', $list);
        }
        return $this->fetch();
    }

    public function update()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->param();
            if (isset($data['id']) && $data['id']) {
                $goods_model = GoodsModel::get($data['id']);
            } else {
                $goods_model = new GoodsModel();
            }
            $admin_id = cmf_get_current_admin_id();
            $admin_info = UserModel::get($admin_id);
            if ($admin_info['company_id']) {
                $goods_model->company_id = $admin_info['company_id'];
            } else {
                $goods_model->company_id = $data['company_id'];
            }
            $goods_model->goods_name = $data['goods_name'];
            $goods_model->goods_img = implode(',', $data['goods_img']);
            $goods_model->price = $data['price'];
            $goods_model->stock = $data['stock'];
            $goods_model->buy_type = $data['buy_type'];
            $goods_model->is_on_sale = $data['is_on_sale'];
            $goods_model->points = isset($data['points']) && $data['points'] ? $data['points'] : 0;
            $goods_model->gift_points = $data['gift_points'];
            $goods_model->goods_detail = $data['goods_detail'];
            $goods_model->cate_id = $data['cate_id'];
            $res = $goods_model->save();
            if ($res) {
                if (isset($data['goods_specs'])) {
                    foreach ($data['goods_specs'] as $key => $value) {
                        if (!isset($value['id'])) {
                            $data['goods_specs'][$key]['goods_id'] = $goods_model->id;
                        }
                    }
                    $goods_spec_model = new  GoodsSpecModel();
                    $goods_spec_model->saveAll($data['goods_specs']);
                }
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
    }

    public function change_status()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $model = GoodsModel::get($data['id']);
            if ($model) {
                $res = GoodsModel::where(['id' => $data['id']])->update([$data['field'] => $data['value']]);
                if ($res) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            } else {
                $this->error('活动不存在或已被删除');
            }
        }
    }

    public function delete()
    {
        $data = $this->request->param();
        $id = isset($data['id']) && $data['id'] ? $data['id'] : 0;
        $goods_info = GoodsModel::where('is_delete', 0)->get($id);
        if (empty($goods_info)) {
            $this->error('商品不存在或已被删除');
        }
        $goods_info->is_delete = 1;
        $res = $goods_info->save();
        if ($res) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    public function user_list()
    {
        $data = $this->request->param();
        $page = isset($data['page']) && $data['page'] ? $data['page'] : 1;
        $admin_id = cmf_get_current_admin_id();
        $admin_info = UserModel::get($admin_id);
        if ($admin_info['company_id']) {
            $where[] = ['company_id', '=', $admin_info['company_id']];
        }
        $list = WechatUserModel::with('company_info')
            ->order('create_time desc')
            ->paginate(10, false, ['page' => $page]);
        $this->success('', '', $list);
    }

    public function category_copy()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->param();
            $old_company_id = $data['old_company_id'];
            $new_company_id = $data['new_company_id'];
            $category_list = CategoryModel::where('company_id', $old_company_id)->all();
            $new_list = [];
            foreach ($category_list as $key => $value) {
                $exits = CategoryModel::where(['company_id' => $new_company_id, 'name' => $value['name']])->find();
                if (empty($exits)) {
                    $category = [];
                    $category['name'] = $value['name'];
                    $category['company_id'] = $new_company_id;
                    $category['sort'] = $value['sort'];
                    $category['status'] = 1;
                    $new_list[] = $category;
                }
            }
            if (empty($new_list)) {
                $this->error('复制失败，目标公司已有全部分类');
            }
            $model = new CategoryModel();
            $res = $model->saveAll($new_list);
            if ($res) {
                $this->success('', '', $new_list);
            } else {
                $this->error('复制失败');
            }

        }
    }

    public function goods_copy()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->param();
            $old_company_id = $data['old_company_id'];
            $new_company_id = $data['new_company_id'];

            $goods_list = GoodsModel::with('goods_specs,category_info')
                ->where(['company_id' => $old_company_id, 'is_delete' => 0])->select();
            foreach ($goods_list as $key => $value) {
                $goods_info = new GoodsModel();
                $goods_info['goods_name'] = $value['goods_name'];
                $goods_info['goods_detail'] = $value['goods_detail'];
                $goods_info['price'] = $value['price'];
                $goods_info['points'] = $value['points'];
                $goods_info['stock'] = $value['stock'];
                $goods_info['gift_points'] = $value['gift_points'];
                $goods_info['is_on_sale'] = $value['is_on_sale'];
                $goods_info['company_id'] = $new_company_id;
                $goods_info['goods_img'] = $value['goods_img'];
                $goods_info['sort'] = $value['sort'];
                $goods_info['buy_type'] = $value['buy_type'];
                if ($value['category_info']) {
                    $category_info = $value['category_info'];
                    $info = CategoryModel::where(['name' => $category_info['name'], 'company_id' => $new_company_id])->find();
                    if ($info) {
                        $goods_info['cate_id'] = $info['id'];
                    } else {
                        $info = new  CategoryModel([
                            'name' => $category_info['name'],
                            'company_id' => $new_company_id,
                            'status' => 1,
                            'sort' => $category_info['sort']
                        ]);
                        $info->save();
                        $goods_info['cate_id'] = $info->id;
                    }
                }
                $goods_info->save();
            }
            $this->success('', '', $goods_list);
        }
    }

    public function category()
    {
        $admin_id = cmf_get_current_admin_id();
        $role_id = RoleUserModel::where('user_id', $admin_id)->value('role_id');
        $role_id = $role_id ? $role_id : 1;
        $this->assign('role_id', $role_id);

        if ($this->request->isAjax()) {
            $data = $this->request->param();
            $where = [];
            $admin_id = cmf_get_current_admin_id();
            $admin_info = UserModel::get($admin_id);
            if ($admin_info['company_id']) {
                $where[] = ['company_id', '=', $admin_info['company_id']];
            } else {
                $company_id = isset($data['company_id']) && $data['company_id'] ? $data['company_id'] : 0;
                if ($company_id) {
                    $where[] = ['company_id', '=', $company_id];
                }
            }
            $list = CategoryModel::with('company_info')->where($where)
                ->order('company_id desc,sort desc')->all();
            $this->success('', '', $list);
        }
        return $this->fetch();
    }

    public function category_update()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->param();
            if (isset($data['id']) && $data['id']) {
                $model = CategoryModel::get($data['id']);
            } else {
                $model = new  CategoryModel();
            }
            $admin_id = cmf_get_current_admin_id();
            $admin_info = UserModel::get($admin_id);
            if ($admin_info['company_id']) {
                $model->company_id = $admin_info['company_id'];
            } else {
                $company_id = isset($data['company_id']) && $data['company_id'] ? $data['company_id'] : 0;
                if ($company_id) {
                    $model->company_id = $company_id;
                }
            }
            $model->name = $data['name'];
            $res = $model->save();
            if ($res) {
                $this->success('更新成功');
            } else {
                $this->error('更新失败');
            }
        }
    }

    public function change_category_status()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $model = CategoryModel::get($data['id']);
            if ($model) {
                $res = CategoryModel::where(['id' => $data['id']])->update([$data['field'] => $data['value']]);
                if ($res) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            } else {
                $this->error('分类不存在或已被删除');
            }
        }
    }

    public function get_category_list()
    {
        $data = $this->request->param();
        $admin_id = cmf_get_current_admin_id();
        $admin_info = UserModel::get($admin_id);
        $where = [];
        if ($admin_info['company_id']) {
            $where[] = ['company_id', '=', $admin_info['company_id']];
        } else {
            $company_id = isset($data['company_id']) && $data['company_id'] ? $data['company_id'] : 0;
            if ($company_id) {
                $where[] = ['company_id', '=', $company_id];
            }
        }
        $list = CategoryModel::where($where)->all();
        $this->success('', '', $list);
    }
}
